package blog.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.service.TagService;

import blog.entity.Tag;

@Controller
@RequestMapping("/admin/tag")
public class TagController {
	@Autowired
	private TagService tagService;
	/**
	 * 	全部标签的显示
	 * 	预留bug：添加的标签名不能重复，且目前没有验证标签名是否重复的操作
	 * @param model
	 * @return
	 */
	@RequestMapping("")
	private String getTagList(Map<String,Object> model) {
		//查询所有标签信息
		List<Tag> tagList = tagService.listTag();
		model.put("tagList", tagList);
		return "Tag/tag-list";
	}
	
	/**
	 * 	添加标签
	 * @param tag
	 * @return
	 */
	@RequestMapping(value="/add",method = RequestMethod.POST)
	private String addTag(Tag tag) {
		//添加标签
		tagService.addTag(tag);
		return "forward:/admin/tag";
	}
	
	/**
	 * 	删除标签
	 * @param tagId
	 * @return
	 */
	@RequestMapping(value = "/delete/{tagId}",method = RequestMethod.GET)
	private String deleteTag(@PathVariable Integer tagId) {
		//删除标签
		tagService.deleteTag(tagId);
		return "forward:/admin/tag";
	}
	
	/**
	 * 	去编辑标签页面
	 * @return
	 */
	@RequestMapping(value="/edit/{tagId}",method = RequestMethod.GET)
	private String goEditTag(@PathVariable Integer tagId,Map<String,Object> model) {
		//根据Id查询要修改的标签
		Tag tag = tagService.getTagById(tagId);
		//查询所有标签信息
		List<Tag> tagList = tagService.listTag();
		
		model.put("Tag", tag);
		model.put("tagList", tagList);
		return "Tag/tag-edit";
	}
	
	/**
	 * 	修改标签
	 * @param tagId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/edit",method = RequestMethod.POST)
	private String editTag(Tag tag) {
		tagService.editTag(tag);
		
		return "forward:/admin/tag";
	}
}
